package mrms.api.service;

import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FaceFeature;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.enums.ImageFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import mrms.api.base.ImageInfo;
import mrms.api.entity.UserEntity;
import mrms.api.repository.UserRepository;
import mrms.api.web.authentication.Encipher;
import mrms.api.web.utils.PicUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author lenchu
 * @date 2019/02/05
 */
@Service
@AllArgsConstructor
public class UserService {

    private final FaceImpl face;
    private final UserRepository userRepository;
    private final ObjectMapper mapper;

    public UserEntity save(UserEntity userEntity) {
        try {
            userEntity.setFaceuri(PicUtil.decode64(userEntity.getFaceuri()));
            userEntity.setFeature(getFeatureByFaceuri(userEntity.getFaceuri()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        userEntity.setPassword(Encipher.encryptPasswordWithMD5(userEntity.getPassword()));
        return userRepository.save(userEntity);
    }

    public UserEntity findUserWithoutFeatureById(String uid) {
        Optional<UserEntity> userEntityOptional = userRepository.findById(uid);
        UserEntity userEntity = userEntityOptional.orElse(new UserEntity());
        userEntity.setFeature(null);
        return userEntity;
    }

    public String transferUserIdToUserEntityJsonString(String uid) {
        Optional<UserEntity> optionalUserEntity = userRepository.findById(uid);
        if (optionalUserEntity.isPresent()) {
            UserEntity userEntity = optionalUserEntity.get();
            try {
                return mapper.writeValueAsString(userEntity);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return uid;
    }

    public byte[] getFeatureByFaceuri(String faceuri) {
        String addId = "6NicX292WuD2npxeHZPaCQSVbfuN6DionSMfu2XRdwep";
        String sdkKet = "HMXxTNqu3rnhbhGZB3wNURFCLVb4tiriTMXpxnytMKHM";

        ImageInfo imageInfo=face.getRGBData(new File(faceuri));
        FaceEngine faceEngine = new FaceEngine();
        faceEngine.active(addId,sdkKet);
        faceEngine.init(face.getengineConfig());
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        faceEngine.detectFaces(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
        FaceFeature faceFeature = new FaceFeature();
        faceEngine.extractFaceFeature(imageInfo.getRgbData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);
        return faceFeature.getFeatureData();
    }
}
